﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using System.Xml.Linq;
using System.Reflection.Metadata;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 插入WMS运单扫码表
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMSWayBill_InPutWMSController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_zh;
        public readonly string con_qh;
        public readonly string con_yb;
        public readonly string con_gz;


        public TMSWayBill_InPutWMSController(ILogger<TMSWayBillInfoRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            con_zh = _configuration.GetConnectionString("WMSDbConnection_ZH");
            con_qh = _configuration.GetConnectionString("WMSDbConnection");
            con_yb = _configuration.GetConnectionString("WMSDbConnection_YB");
            con_gz = _configuration.GetConnectionString("WMSDbConnection_GZ");
            _logger = loggerFactory.CreateLogger<TMSWayBill_InPutWMSController>();
        }

        /// <summary>
        /// 插入WMS运单扫码表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostCreateWMSWayBillInfo([FromBody] TMSWayBill_InPutWMSRequestDto strparm)
        {
            string strMsg = "";
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            TMSWayBill_InPutWMSResponseDto re = new TMSWayBill_InPutWMSResponseDto();
            string waybill_no = strparm.waybill_no;
            string shippingorder_no = strparm.shippingorder_no;
            string in_con = "";//目标WMS数据库
            try
            {
                if (waybill_no == "")
                {
                    strMsg = "w_plan_code must be not null";
                }
                if (strparm.factory_code == "")
                {
                    strMsg = "factory_code must be not null";
                }
                else
                {
                    if (strparm.factory_code.Contains("金湾"))
                    {
                        in_con = con_zh;
                    }
                    else
                    {
                        if (strparm.factory_code.Contains("青海"))
                        {
                            in_con = con_qh;
                        }
                        else
                        {
                            if (strparm.factory_code.Contains("四川"))
                            {
                                in_con = con_yb;
                            }
                            else
                            {
                                if (strparm.factory_code.Contains("高臻"))
                                {
                                    in_con = con_gz;
                                }
                                else
                                {
                                    strMsg = "factory_code=<" + strparm.factory_code + "> not find ";
                                }
                            }
                        }
                    }
                }
                if (strMsg == "")
                {              
                    Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic

                    dic = new Dictionary<string, string>(); //查询公用dic
                    dic.Add("ccode", shippingorder_no);//查询对应物流计划
                    DataTable yd_dt = MethodHelp.GetTableInfo("TMS0023", dic, con);//获取物流计划信息
                    if (yd_dt.Rows.Count>0)
                    {
                        dic = new Dictionary<string, string>();
                        dic.Add("waybill_no", waybill_no);//查询对应物流计划
                        DataTable wl_dt = MethodHelp.GetOutsideTableInfo("TMS0021", dic, in_con, con);//获取物流计划信息
                        if (wl_dt.Rows.Count > 0)
                        {
                            foreach (DataRow item in wl_dt.Rows)
                            {
                                dic = new Dictionary<string, string>();
                                dic.Add("shipping_order_no", yd_dt.Rows[0]["waybill_no"].ToString());//查询对应物流计划
                                dic.Add("shipment_no", item["shipment_no"].ToString());
                                dic.Add("sales_order_line_id", item["source_item_code"].ToString());
                                string strSQL = MethodHelp.GetTranSQL("TMS0022", dic, con);
                                if (strSQL == "")
                                {
                                    strMsg = "TMS0022,Parameter conversion failed";
                                }
                                else
                                {
                                    strSqlList.Add(strSQL.ToString());
                                }
                            }
                            if (strSqlList.Count > 0)
                            {
                                if (strMsg == "")
                                {
                                    bool A = DB.ExecuteNoQueryTran(strSqlList, in_con);
                                    if (A)
                                    {

                                    }
                                    else
                                    {
                                        strMsg = " Database insert failed";
                                    }
                                }
                            }
                            else
                            {
                                strMsg = "Handle No insertion data present";
                            }


                        }
                        else
                        {
                            strMsg = "waybill_no=<" + waybill_no + "> info  not find";
                        }
                    }
                    else
                    {
                        strMsg = "TMS找不到运单=<" + shippingorder_no + "> 信息";
                    }        
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

            if (strMsg == "")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;

            }


            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);

        }


    }
}
